package yanyan.com.tochar.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class HttpUtil {
  private static  OkHttpClient okHttpClient=new OkHttpClient();
    public static Call getMsg(String url) {
        Request request = new Request.Builder().url(url).get().build();
        Call call =okHttpClient.newCall(request);
        return call;
    }


    /**
     * 获取网页源码
     * @param url
     * @return
     */
    public static String getHtml(String url){
        BufferedReader bf=null;
        String res="";
        try {
            URL u=new URL(url);
            bf=new BufferedReader(new InputStreamReader(u.openStream()));
            StringBuffer sb=new StringBuffer();
            String line;
            while ((line=bf.readLine())!=null){
                sb.append(line);
            }
            res=sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                bf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return res;
    }
}
